package com.example.carbon.controller;

import com.example.carbon.pojo.dto.BomManagerDto;
import com.example.carbon.pojo.dto.PageLimit;
import com.example.carbon.pojo.vo.RestBean;
import com.example.carbon.server.info.BomManagerServer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/bom")
@Slf4j
public class BomManagerController {
    @Resource
    BomManagerServer bomManagerServer;

    /**
     * 获取bom数据
     * @param page
     * @param item
     * @param name
     * @return
     */
    @RequestMapping("/get/{page}/{item}")
    public RestBean selectAllInfo(@PathVariable("page") int page, @PathVariable("item") int item, String name) {
        try {
            PageLimit pageLimit = new PageLimit(page, item);
            return RestBean.success(bomManagerServer.selectAllInfo(pageLimit, name));
        } catch (Exception e) {
            return RestBean.failure("获取数据失败");
        }
    }

    /**
     * 删除bom表单数据
     * @param id
     * @return
     */
    @RequestMapping("/del/{id}")
    public RestBean delInfo(@PathVariable("id") int id) {
        try {
            if (bomManagerServer.delInfoById(id)) {
                return RestBean.success("删除成功");
            }
            return RestBean.failure("删除失败");
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("请联系管理员");
        }
    }

    /**
     * 更新数据
     * @param bomManagerDto
     * @return
     */
    @PostMapping("/update")
    public RestBean updateNewInfo(@RequestBody BomManagerDto bomManagerDto) {
        try {
            log.info(bomManagerDto.toString());
            if (bomManagerServer.updateNewInfo(bomManagerDto)) return RestBean.success("修改成功");
            return RestBean.failure("修改失败");
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("修改失败,请联系管理员");
        }
    }

    /**
     * 插入一条新的bom表单数据
     * @param bomManagerDto
     * @return
     */
    @RequestMapping("/insert")
    public RestBean insertNewInfo(@RequestBody BomManagerDto bomManagerDto) {
        try {
            if (bomManagerServer.insertNewInfo(bomManagerDto)) {
                return RestBean.success("增加成功");
            }
            return RestBean.failure("增加失败");
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("增加失败，请联系管理员");
        }
    }

    /**
     * 通过bomId来获取与之相关联的物料信息
     * @param id
     * @return
     */
    @RequestMapping("/rel/{id}")
    public RestBean selectRelByBomId(@PathVariable("id") int id) {
        try {
            return RestBean.success(bomManagerServer.selectRelByBomId(id));
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("获取信息失败");
        }
    }

    /**
     * 在修改之前要重新获取一次数据信息
     * @param id
     * @return
     */
    @RequestMapping("set/{id}")
    public RestBean selectBomInfoById(@PathVariable("id") int id) {
        try {
            return RestBean.success(bomManagerServer.selectBomInfoById(id));
        } catch (Exception e) {
            log.info(e.getMessage());
            return RestBean.failure("获取信息失败");
        }
    }
}
